/*
 * Copyright (c) 2023 IUV Inc. All rights reserved.
 * 项目名称：IUV TXY
 * 文件名：DemoBaikeBelongServiceImpl.java
 * Date：23-10-10 下午2:09
 * Author：Will
 */

package com.iuv.baike.service.impl;

import com.aliyun.dytnsapi20200217.models.DescribePhoneNumberOperatorAttributeResponse;
import com.aliyun.dytnsapi20200217.models.DescribePhoneNumberOperatorAttributeResponseBody;
import com.iuv.baike.domain.DemoBaikeBelongTbl;
import com.iuv.baike.dto.BelongInfo;
import com.iuv.baike.repository.DemoBaikeBelongTblRepository;
import com.iuv.baike.service.DemoBaikeBelongService;
import com.iuv.baike.util.HaoMaBaiKeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * DemoBaikeBelongServiceImpl
 * 号码百科号码归属地例子-业务实现类
 *
 * @author Will
 * @date 13:50
 */
@Service("demoBaikeBelongService")
public class DemoBaikeBelongServiceImpl implements DemoBaikeBelongService {
    @Autowired
    private DemoBaikeBelongTblRepository demoBaikeBelongTblRepository;

    @Override
    public BelongInfo queryOverYun(String mobile) {

        DescribePhoneNumberOperatorAttributeResponse describePhoneNumberOperatorAttributeResponse =HaoMaBaiKeUtil.queryNumberStatus(mobile);
        if(describePhoneNumberOperatorAttributeResponse!=null)
        {
            DescribePhoneNumberOperatorAttributeResponseBody body=describePhoneNumberOperatorAttributeResponse.getBody();
            if("OK".equalsIgnoreCase(body.getCode()))
            {
                DescribePhoneNumberOperatorAttributeResponseBody.DescribePhoneNumberOperatorAttributeResponseBodyData data=body.getData();
                if(data!=null) {
                    DemoBaikeBelongTbl demoBaikeBelongTbl=new DemoBaikeBelongTbl();
                    demoBaikeBelongTbl.setBasicCarrier(data.getBasicCarrier());
                    demoBaikeBelongTbl.setMobile(mobile);
                    demoBaikeBelongTbl.setCarrier(data.getCarrier());
                    demoBaikeBelongTbl.setNumberPortability(data.isNumberPortability?1:0);
                    demoBaikeBelongTbl.setNumberSegment(""+data.getNumberSegment());
                    demoBaikeBelongTbl.setCity(data.getCity());
                    demoBaikeBelongTbl.setProvince(data.getProvince());
                    Date now = new Date();
                    demoBaikeBelongTbl.setCreateTime(now);
                    demoBaikeBelongTbl.setLastUpdateTime(now);
                    demoBaikeBelongTblRepository.save(demoBaikeBelongTbl);
                    return changeTo(demoBaikeBelongTbl);
                }
            }
        }
        return null;
    }

    @Override
    public BelongInfo queryByMobile(String moblie) {
        if(moblie!=null)
        {
            DemoBaikeBelongTbl demoBaikeOnlineHourTbl= demoBaikeBelongTblRepository.queryTopByMobile(moblie);
            if(demoBaikeOnlineHourTbl!=null)
            {
                return changeTo(demoBaikeOnlineHourTbl);
            }
        }
        return null;
    }

    private BelongInfo changeTo(DemoBaikeBelongTbl demoBaikeBelongTbl)
    {
        return new BelongInfo(demoBaikeBelongTbl.getMobile(),demoBaikeBelongTbl.getBasicCarrier(),demoBaikeBelongTbl.getCarrier(),demoBaikeBelongTbl.getNumberPortability(),demoBaikeBelongTbl.getNumberSegment(),demoBaikeBelongTbl.getCity(),demoBaikeBelongTbl.getProvince());
    }

}
